﻿import Request from '../../base/request.js?t=202511041006'
import Storage from '../../base/storage.js?t=202511041006'
import { imageBaseUrl, getRecentYears, getBrowserInfo } from "../../base/utils.js?t=202511041006"
const Composition = (proxy) => {

    const generateh = (text, isShort) => {
        if (isShort) {
            return h('div', {
                style: "width:100px;overflow:hidden;white-space: nowrap;text-overflow: ellipsis; -o-text-overflow: ellipsis;cursor:pointer"
            }, { default: () => text })
        } else {
          return h('div', {
                style: "width:300px;height:auto;"
            }, { default: () => text })
        }

    }

    const data = reactive({
        messageTest: "",
        proxy: "",
        queryParams: {
            year: null,
            person: "",
            title: null,
            status: null
        },
        statusList: [
            { name: '暂存', code: '0' },
            { name: '待批准', code: '1' },
            { name: '办结', code: '2' },
        ],
        yearList: [
       
        ],
        pagination: {
            total: 0,
            current: 1,
            loading: false,
            pageSize: 10,
            showTotal: total => `总共 ${total} 项`,
            defaultPageSize: 10,
            showSizeChanger: true, // 是否显示pagesize选择
            showQuickJumper: false, // 是否显示跳转窗
   
        },
        columns: [
            {
                title: '序号',
                dataIndex: 'a',
                key: 'a',
                align: 'center',
                width: 80,
                customRender: ({ index}) => {
                    return (params.pagination.value.current - 1) * params.pagination.value.pageSize + index + 1
                },
            },
            {
                title: '审核名称',
                dataIndex: 'NoticeTitle',
                key: 'NoticeTitle',
                align: 'center'

            },
            {
                title: '受审部门',
                dataIndex: 'DepartmentList',
                key: 'DepartmentList',
                align: 'center',
                customRender: function ({ text, record }) {
                    let content = h('div', {
                        style: 'width:700px'
                    }, { default: () => record.DepartmentList.map(e => e.DepartmentName).toString() });


           
                    let textc = h(antd.Button, {
                        type: "link"
                    }, { default: () => record.DepartmentList.length });

                    return h(antd.Popover, {
                        title: '受审部门',
                        content: content,
  
                    }, () => [textc] );
                }
            },
            {
                title: '审核目的',
                dataIndex: 'TargetName',
                key: 'TargetName',
                align: 'center',
                customRender: function ({ record }) {
       

                    return h(antd.Popover, {
                        title: '审核目的',
                        content: generateh(record.TargetName && record.TargetName[0].IsInput == true ? record.OtherTarget : record.TargetName[0].ConfigName)
                    }, () => [generateh(record.TargetName && record.TargetName[0].IsInput == true ? record.OtherTarget : record.TargetName[0].ConfigName, true)] );

                   
              
              
                }
            },
            {
                title: '审核日期',
                dataIndex: 'StartTime',
                key: 'StartTime',
                align: 'center',
                customRender: function ({ text, record}) {
                    const regex = /\/Date\((\d+)\)\//;
                    const match1 = text && record.StartTime.match(regex);
                    const match2 = text && record.EndTime.match(regex);
                    if (match1 && match2) {

                        let sTag = h(antd.Tag, {
                            color:"green"
                        }, { default: () => dayjs(parseInt(match1[1], 10)).format('YYYY-MM-DD') });

                        let eTag = h(antd.Tag, {
                            color:"red"
                        }, { default: () => dayjs(parseInt(match2[1], 10)).format('YYYY-MM-DD') });

                        let divide = h('div', {
                     
                        }, { default: () => '-' });

                        return h(antd.Space, {
                           
                        }, () => [sTag,divide,eTag ]);
                    } else {
                        return ""
                    }
                }
            },
            {
                title: '填报人',
                dataIndex: 'CreateUserName',
                key: 'CreateUserName',
                align: 'center'
            },
            {
                title: '状态',
                dataIndex: 'StatusName',
                key: 'StatusName',
                align: 'center',
                width:'100px',
                customRender: function ({ text, record }) {
                    let steps = h(antd.Steps, {
                        type: "inline",
                        current: +record.Step - 1,
                        items: [
                            {
                                title: '填写内部审核计划信息',
                                description: '填写内部审核计划信息',
                            },
                            {
                                title: '设置审核组',
                                description: '设置审核组',
                            },
                            {
                                title: '设置审核日程',
                                description: '设置审核日程',
                            },
                            {
                                title: '发布年度考核计划',
                                description: '发布年度考核计划',
                            },
                        ].map(item => ({ key: item.title, title: item.title, description: item.description })),
                    }, { default: () => text });
                    let textc = h(antd.Tag, {
                        color: text == '保存' ? "#87d068" : "#108ee9",
                        style:'cursor:pointer'
                    }, { default: () => text });

                    return h(antd.Popover, {
                        title: '当前节点',
                        content: steps,
                    }, () => [textc]);


                }
            },
            {
                title: '备注',
                dataIndex: 'Remark',
                key: 'Remark',
                align: 'center',
                customRender: function ({ record }) {


                    return h(antd.Popover, {
                        title: '备注',
                        content: generateh(record.Remark)
                    }, () => [generateh(record.Remark, true)]);




                }
            },


            
            {
                title: '操作',
                key: 'action',
                align: 'center',
                fixed: 'right',
                width: 100,
                customRender: function ({ text, record, index, column }) {
               
                    const returnBtn = h(antd.Button, {
                        style: 'margin-right:4px',
                        type: "link"
                    }, { default: () => '撤回' })

                    const fReturnBtn = h(antd.Popconfirm, {
                        title: "确定撤回此数据？",
                        onConfirm: () => methods.handleReturn(record.Id)

                    }, { default: () => returnBtn })



                    const delBtn = h(antd.Button, {
                        style: 'margin-right:4px',
                        type: "link"
                    }, { default: () => '删除' })

                    const fDelBtn = h(antd.Popconfirm, {
                        title: "确定删除此数据？",
                        onConfirm: () => methods.handleDelete(record.Id)

                    }, { default: () => delBtn })


                    const modifyBtn = h(antd.Button, {
                        style: 'margin-right:4px',
                        type: "link",
                        onClick: () => methods.handleModify(record)
                    }, { default: () => '修改' })




                    const detailBtn = h(antd.Button, {
                        style: 'margin-right:4px',
                        type: "link",
                        onClick: () => methods.handleDetail(record)
                    }, { default: () => '详情' })

                    const approvalBtn = h(antd.Button, {
                        style: record.IsApprover? 'margin-right:4px':'display:none',
                        type: "link",
                        onClick: () => {
                            methods.handleApproval(record)
                        }
                    }, { default: () => '审核' })





                    if (record.StatusName !== '保存') {
                        return h(antd.Space, {
                        },
                            () => [fReturnBtn, fDelBtn, detailBtn , approvalBtn]
                        );

                    } else { 
                        return h(antd.Space, {
                        },
                            () => [fReturnBtn, fDelBtn, modifyBtn, detailBtn]
                        );
                    }

                }
            },
        ],
        dataSource: [],
    })
    let params = toRefs(data)
    const methods = {
        handleReturn: (id) => {

            Request({
                url: "/performance/WithdrawPlan",
                method: 'post',
                params: {
                    FormId: id,
                },
            }).then(res => {
                methods.getTableList(true)
                params.messageTest.value.info(res.Msg);
            })
        },
        handleDelete: (id) => {
            Request({
                url: "/performance/DeletePlan",
                method: 'post',
                params: {
                    FormId: id,
                },
            }).then(res => {
                methods.getTableList(true)
                params.messageTest.value.info(res.Msg);
            })
        },
        handleModify: (record) => {

            const { Step, Id } = record
            window.location.href = `/Performance/PerformancePlanAdd?id=${Id}&step=${Step-1}`




        },

        handleDetail: (record) => {

            const {  Id } = record


            window.location.href = `/Performance/PerformancePlanDetail?id=${Id}`




        },





  
        addPlan: () => {
            window.location = "/Performance/PerformancePlanAdd";
        },
        handleTableChange: (page) => {
            const { current, pageSize } = page
            params.pagination.value.current = current;
            params.pagination.value.pageSize = pageSize;
            methods.getTableList()
        },
        getTableList:(isRresh) => {
            if (isRresh) {
                params.pagination.value.current = 1
            }
            Request({
                url: "/performance/GetPerformancePlanPageList",
                method: 'post',
                params: {
                    page: params.pagination.value.current,
                    pageSize: params.pagination.value.pageSize
                },
            }).then(res => {
                params.dataSource.value = res.Data.Data
                params.pagination.value.total = res.Data.TotalItemCount
                params.pagination.value.loading = false
            })
        },

        init: () => {
              getBrowserInfo().catch(err => { console.log(err)})
            if (proxy) {
              
                params.proxy.value = proxy
            }
            const { message } = antd
            params.messageTest.value = message
            params.messageTest.value.config({
                top: `10vh`,
                duration: 7,
                maxCount: 1,
                rtl: true,
            });

            params.pagination.value.loading = true

            params.yearList.value = getRecentYears()
            let info = Storage.get('AccountInfo', true)
            Request({
                url: "/Account/createJwtToken",
                method: 'post',
                data: {
                    "userName": info && info.UserNo
                }
            }).then(res => {
                Storage.set('token', res.Data,true)
                methods.getTableList(true)
                
            })
        },
        handleApproval: (record) => {
            window.location.href = `/Performance/PerformanceRtifyPlan?id=${record.Id}`
        }
    }
    const template = {
        test: {
            template: `<div @click="add">+</div> <div>{{testnum}}</div>`,

            setup() {

                return {
                    ...params,
                    ...methods,
                };
            }

        }
    }
    return {
        ...params,
        ...methods,
        template
    }
}
export default Composition